package com.huifaapp.mall.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.GoodstoboActivity;
import com.huifaapp.mall.adapter.GoodsReceivedAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.GoodsReceivedBean;
import com.huifaapp.mall.bean.event.OrderStatusEventMessage;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 待收货
 * A simple {@link Fragment} subclass.
 */
public class GoodsReceivedFramgnet extends Fragment {


    private RecyclerView GoodsReceive;
    private List<GoodsReceivedBean.OrderListBean> list;
    private GoodsReceivedAdapter goodsReceivedAdapter;


    public GoodsReceivedFramgnet() {
        // Required empty public constructor
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods_received_framgnet,container,false);
        initView(view);
        GoodsReceive.setLayoutManager(new LinearLayoutManager(getActivity()));
        EventBus.getDefault().register(this);
        return view;
    }



    @Subscribe
    public void onEvent(OrderStatusEventMessage orderStatusEventMessage){
        Log.d("NIHAOA", "onEvent: " + orderStatusEventMessage.toString());
        for (int i = 0;i<list.size();i++){
            if (list.get(i).getOrderNo().equals(orderStatusEventMessage.getOrderNo())) {
                list.remove(i);
                goodsReceivedAdapter.notifyDataSetChanged();
                break;
            }
        }
    }

    private boolean isGetData = false;
    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        //   进入当前Fragment
        if (enter && !isGetData) {
            isGetData = true;
            OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notrecieved")
                    .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                    .addParams("appId", AUniqueIdentifier.getDeviceId(getActivity()))
                    //"token=201712291124171334f3ed61d93e05404b8dc78f10c4770cf8&appId=D7B72681-8205-4C8F-8CCB-142C448203E3")
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {

                        }

                        @Override
                        public void onResponse(String response) {

                            Log.i("goodsreHB",response);
                            try {
                                JSONObject jsonObject = new JSONObject(response);

                                JSONArray orderList = jsonObject.getJSONArray("orderList");
                                Log.e("FH",orderList+"");
                                initData(response);
                            } catch (JSONException e) {
                                Log.e("FH","HHH");
                            }

                        }
                    });

            //   这里可以做网络请求或者需要的数据刷新操作
        } else {
            isGetData = false;
        }
        return super.onCreateAnimation(transit, enter, nextAnim);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isGetData) {
            OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notrecieved")
                    .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                    .addParams("appId", AUniqueIdentifier.getDeviceId(getActivity()))
                    //"token=201712291124171334f3ed61d93e05404b8dc78f10c4770cf8&appId=D7B72681-8205-4C8F-8CCB-142C448203E3")
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {

                        }

                        @Override
                        public void onResponse(String response) {

                            Log.i("goodsreHB",response);
                            try {
                                JSONObject jsonObject = new JSONObject(response);

                                JSONArray orderList = jsonObject.getJSONArray("orderList");
                                Log.e("FH",orderList+"");
                                initData(response);
                            } catch (JSONException e) {
                                Log.e("FH","HHH");
                            }

                        }
                    });

            //   这里可以做网络请求或者需要的数据刷新操作
            isGetData = true;
        }
    }
    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        isGetData = false;
        super.onDestroy();
    }


    private void initView(View view) {
        GoodsReceive = (RecyclerView) view.findViewById(R.id.goodsreceive_rv);
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/notrecieved")
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(getActivity()))
                //"token=201712291124171334f3ed61d93e05404b8dc78f10c4770cf8&appId=D7B72681-8205-4C8F-8CCB-142C448203E3")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {

                        Log.i("goodsreHB",response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);

                            JSONArray orderList = jsonObject.getJSONArray("orderList");
                            Log.e("FH",orderList+"");
                            initData(response);
                        } catch (JSONException e) {
                            Log.e("FH","HHH");
                        }

                    }
                });

//        initData("{}");

    }

    private void initData(String response) {
        Gson gson = new Gson();
//        PendingPayBean pendingPayBean = gson.fromJson(response, PendingPayBean.class);
        final GoodsReceivedBean goodsReceivedBean = gson.fromJson(response,GoodsReceivedBean.class);
        list = goodsReceivedBean.getOrderList();
//        list = new ArrayList<>();
//        for (int i = 0 ;i<=3;i++){
//            GoodsReceivedBean.OrderListBean orderListBean = new GoodsReceivedBean.OrderListBean();
//            orderListBean.setOrderNo("201801113300" + i);
//            orderListBean.setExpressFee(12.03);
//            orderListBean.setTotalcount(111);
//            orderListBean.setTotalprice(1111);
//            orderListBean.setData(new ArrayList<GoodsReceivedBean.OrderListBean.DataBean>());
//            list.add(orderListBean);
//        }

        goodsReceivedAdapter = new GoodsReceivedAdapter(getActivity(), list);
        GoodsReceive.setAdapter(goodsReceivedAdapter);
        goodsReceivedAdapter.setOnItemClickListener(new GoodsReceivedAdapter.MyOnItemClickListener() {
            @Override
            public void OnItemClickListener(View view, int position) {

            }

            @Override
            public void Btconfirm(View view, int position) {
                final  GoodsReceivedBean.OrderListBean orderListBean = list.get(position);
                Intent intent = new Intent(getActivity(), GoodstoboActivity.class);
                intent.putExtra("orderNo",orderListBean.getOrderNo());
                Log.e("jijijijijijij",orderListBean.getOrderNo());
                startActivity(intent);
            }
        });
    }


}
